package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.PhoneUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.api.MerchantService;
import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.merchant.converter.MerchantDetailConvert;
import com.huiminpay.merchant.converter.MerchantRegisterConvert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.dto.MerchantDTO;
import com.huiminpay.merchant.service.FileService;
import com.huiminpay.merchant.service.SmsService;
import com.huiminpay.merchant.vo.MerchantDetailVo;
import com.huiminpay.merchant.vo.MerchantRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 有点儿意思
 * @Description:
 */
@RestController
@Api(tags = "商户服务接口")
public class MerchantController {

    @Reference
    MerchantService merchantService;

    @Autowired
    SmsService smsService;

    @Autowired
    FileService fileService;

    @Reference
    AppService appService;



    @GetMapping("/merchants/{id}")
    @ApiOperation("根据id查询商户")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long merchantId){
        return merchantService.queryMerchantById(merchantId);
    }

    @ApiOperation("生成验证码")
    @GetMapping("/sms")
    public String sendCode(@RequestParam String  phone){
        return smsService.sendSms(phone);
    }

    @ApiOperation("注册商户")
    @ApiImplicitParam(name = "merchantRegister", value = "注册信息", required = true, dataType =
            "MerchantRegisterVO", paramType = "body")
    @PostMapping("/merchants/register")
    public MerchantRegisterVo registerMerchant(@RequestBody MerchantRegisterVo registerVo){
        // 1.校验
        if (registerVo == null) {
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        // 手机号非空校验
        if (StringUtils.isBlank(registerVo.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        // 校验手机号的合法性
        if (!PhoneUtil.isMatches(registerVo.getMobile())) {
            throw new BusinessException(CommonErrorCode.E_100109);
        }
        // 联系人非空校验
        if (StringUtils.isBlank(registerVo.getUsername())) {
            throw new BusinessException(CommonErrorCode.E_100110);
        }
        // 密码非空校验
        if (StringUtils.isBlank(registerVo.getPassword())) {
            throw new BusinessException(CommonErrorCode.E_100111);
        }
        // 验证码非空校验
        if (StringUtils.isBlank(registerVo.getVerifiyCode()) ||
                StringUtils.isBlank(registerVo.getVerifiykey())) {
            throw new BusinessException(CommonErrorCode.E_100103);
        }
        //校验验证码
        smsService.checkCode(registerVo.getVerifiykey(), registerVo.getVerifiyCode());
        //注册商户
        MerchantDTO merchantDTO = MerchantRegisterConvert.INSTANCE.vo2Dto(registerVo);
        merchantService.createMerchant(merchantDTO);
        //返回结果
        return registerVo;
    }

    @ApiOperation("上传文件到阿里云")
    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file){
        return fileService.upload(file);
    }

    @ApiOperation("资质申请")
    @PostMapping("/my/merchants/save")
    public void saveMerchant(@RequestBody MerchantDetailVo merchantDetailVo){
        //获取用户id
        Long merchantId = SecurityUtil.getMerchantId();
        MerchantDTO merchantDTO = MerchantDetailConvert.INSTANCE.vo2dto(merchantDetailVo);
        merchantService.applyMerchant(merchantId,merchantDTO);
    }

    @ApiOperation("查询应用列表")
    @GetMapping(value = "/my/apps")
    public List<AppDTO> queryMyApps() {
        Long merchantId = SecurityUtil.getMerchantId();
        return appService.queryByMerchantId(merchantId);
    }

    @ApiOperation("获取登录用户的商户信息")
    @GetMapping(value="/my/merchants")
    public MerchantDTO getMyMerchantInfo(){
        // 从token 中获取商户id
        Long merchantId = SecurityUtil.getMerchantId();
        return merchantService.queryMerchantById(merchantId);
    }
}
